package cate.game.attr;

import cate.common.table.item.equip.row.*;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.bag.equip.Equip;
import cate.game.role.bag.equip.EquipsOn;

import java.util.*;

public class EquipsPowerAttrPart extends PowerAttrPart {
    public EquipsPowerAttrPart() {
    }

    public EquipsPowerAttrPart(PlayBuildContext ctx, EquipsOn po) {
        FightAttr attr = new FightAttr();
        List<Integer> skills = new ArrayList<>();
        if (po.equipMap != null) {
            Map<Integer, Integer> suites = new HashMap<>();
            Map<Integer, Integer> godSuites = new HashMap<>();
            for (Equip equip : po.equipMap.values()) {
                EquipBaseRow row = equip.getTpl(ctx);
                if (row == null) {
                    continue;
                }
                FightAttr equipAttr = equip.getEquipAttr(ctx, po.owner);
                attr.add(equipAttr);
                attr.add(equip.psionicAttr(ctx, po.owner));
                if (row.canPump == 1) {
                    if (row.suiteType > 0) {
                        godSuites.merge(row.suiteType, 1, Integer::sum);
                    }
                } else {
                    if (row.suiteType > 0) {
                        suites.merge(row.suiteType, 1, Integer::sum);
                    }
                }
            }
            int godTypeNum = godSuites.values().stream().mapToInt(value -> value).sum();
            //如果四件都是神装
            if (godTypeNum >= 4) {
                OptionalInt minTypeOp = godSuites.keySet().stream().mapToInt(v -> v).min();
                int minType = minTypeOp.getAsInt();
                List<EquipSuiteRow> esTpl = ctx.equipExcel().suite.find(minType, 4);
                if (esTpl != null) {
                    for (EquipSuiteRow row : esTpl) {
                        attr.add(new FightAttr(row.attrStr));
                    }
                }
            } else {
                //如果身上有非神装，则神装都算作最高级的非神装
                if (suites.size() > 0) {
                    Integer mostKey = suites.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(0);
                    if (mostKey != 0 && suites.get(mostKey) > 1) {
                        for (int val : godSuites.values()) {
                            suites.merge(mostKey, val, Integer::sum);
                        }
                    } else {
                        int maxType = suites.keySet().stream().mapToInt(v -> v).max().orElse(0);
                        if (maxType > 0) {
                            for (int val : godSuites.values()) {
                                suites.merge(maxType, val, Integer::sum);
                            }
                        }
                    }

                    // 套装属性
                    suites.forEach(
                            (suiteType, num) -> {
                                List<EquipSuiteRow> esTpl = ctx.equipExcel().suite.find(suiteType, num);
                                if (esTpl != null) {
                                    for (EquipSuiteRow row : esTpl) {
                                        attr.add(new FightAttr(row.attrStr));
                                    }
                                }
                            });
                }
            }
        }
        this.attr = attr;
        this.powerExt = 0;
        skills.addAll(po.getSkills());
        this.skills = skills;
        if (po.equipMap != null) {
            for (Equip equip : po.equipMap.values()) {
                if (equip.gemsOn != null) {
                    this.merge(equip.gemsOn.getPowerAttrPart(ctx, po.owner));
                }
            }
        }
    }
}
